//三
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    max: [0],
    min: [0],
    aniamtion: '',
    miaoshu: 6,
    state: 0,
    _max: 0,
    _min: 0,
    height: 0,
    imgurl: app.globalData.url,
    change_img: app.globalData.url + '/wxaShop/pages/img/yx19.png',
    cloth: 0,
    inputlist: [{ "placeholder": "0", focus: true, value: '' }, { "placeholder": "0", focus: false, value: '' }],
    output: 0,
    inputState: 0,
    arrs: [],
    message: '',
    rewad: 100,
    detail: '',
    rewardNum_two: 0,
    rewardNum_one: 0,
    animation1:'',
    marginTop:'0rpx',
  },
  // 返回
  back: function () {
    wx.navigateBack({
      delta: 1
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    this.setData({
      commodityInfo: wx.getStorageSync('commodityInfo'),
    });

    this.randoms()
  },
  // 获取最小值到最大值之前的整数随机数
  random: function (Min, Max) {
    var Range = Max - Min;
    var Rand = Math.random();
    return (Min + Math.round(Rand * Range));
  },
  // 方法
  randoms: function () {
    let that = this
    // 最小值
    let minarr = this.data.min
    let arr1 = minarr.push(that.random(0, 9))
    // 最大值
    let maxarr = this.data.max
    let arr2 = maxarr.push(that.random(0, 9))

    that.setData({ min: minarr, max: maxarr })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let that = this
    this.animation = wx.createAnimation({
      duration: 300,
      timingFunction: 'linear'
    })
    this.animation1 = wx.createAnimation({
      duration: 300,
      timingFunction: 'linear'
    })
    // 用jquery 获取 每个view的高
    var query = wx.createSelectorQuery()
    query.select('#gethight').boundingClientRect()
    query.exec(function (res) {
      that.setData({
        height: res[0].height
      })
    })
  },
  translate: function () {
    let that = this
    var num = 1
    let miao = 6
    //清空动画的位移
    this.animation.translate(0, (0)).step()
    this.setData({ animation: this.animation.export() })
    this.animation1.translate(0, (0)).step()
    this.setData({ animation1: this.animation1.export() })
    this.setData({
      change_img: app.globalData.url + '/wxaShop/pages/img/yx16.gif',
      min: [1],
      max: [1],
    })
    var aaa = setInterval(function () {
      let minarr = that.data.min
      let maxarr = that.data.max
      let minarr1 = minarr.push(that.random(0, 9))
      let maxarr1 = maxarr.push(that.random(0, 9))
      that.animation.translate(0, -(that.data.height * (num - 1))).step()
      that.setData({ animation: that.animation.export(), min: minarr, max: maxarr })
      num += 1;
    }, 450)
    var aaaa =''
    setTimeout(function(){
          aaaa = setInterval(function () {
          let minarr = that.data.min
          let maxarr = that.data.max
          let minarr1 = minarr.push(that.random(0, 9))
          let maxarr1 = maxarr.push(that.random(0, 9))
          that.animation1.translate(0, -(that.data.height * (num - 1))).step()
          that.setData({ animation1: that.animation1.export(), min: minarr, max: maxarr })
          num += 1;
        }, 450)
        
    },100)
    
    //6秒执行完 就清空 滚动的循环和时间
    var bbb = setInterval(function () {
      miao -= 1
      //秒数=0 就改变图片 
      if (miao <= 0) {
        that.setData({
          change_img: app.globalData.url + '/wxaShop/pages/img/yx19.png',
          inputState: 1,
        })
        // 秒数等于 0 就调用输入时用的方法
        that.miao()
        //清空
        clearInterval(bbb)
        clearInterval(aaa)
        clearInterval(aaaa)
        console.log(that.data.min,that.data.max)
      }
      that.setData({ miaoshu: miao,})

    }, 1000)
    setTimeout(function(){
      console.log(1111)
        that.setData({
          marginTop:'170rpx'
        })
    },3000)
  },
  // 点击秒数
  miao: function () {
    let that = this
    let miao1 = 10
    let arr = this.data.max
    console.log("max" + arr)
    let arr1 = this.data.min
    console.log("min" + arr1)
    this.setData({
      state:1
    })
    var ccc = setInterval(function () {
      miao1 -= 1
      if (miao1 <= 0) {
        wx.showTabBar({
          title: '时间到',
          icon: 'none'
        })
        setTimeout(()=>{  
          wx.vibrateLong({success:function(e){
              console.log(e)
          },fail:function(error){
            console.log(error)
          }});
        },1500)
        clearInterval(ccc)        
        if (that.data.message == '猜对了') {
          that.setData({
            cloth: 1,
          })
        }
        if (that.data.message == '低了') {
          that.setData({
            cloth: 3,
          })
          that.getCoupon(100)
        }
        if (that.data.message == '高了') {
          that.setData({
            cloth: 3,
          })
          that.getCoupon(100)
        }
        that.setData({
          inputState: 0,
          arrs: [],
          state: 0,
          marginTop:'0rpx',
        })
      }
      that.setData({ miaoshu: miao1, })
    }, 1000)

  },
  // 第一个输入框
  output: function (e) {
    let that = this
    //获取要输入是时的数组
    var arr = this.data.arrs
    let list = this.data.inputlist
    let index = e.currentTarget.id
    //第一个值
    var one = this.data.min[this.data.min.length - 3]
    //第二个值
    var two = this.data.max[this.data.max.length - 3]
    var indexv1 = 'inputlist[' + 0 + '].focus'
    var indexv = 'inputlist[' + 1 + '].focus'
    var value1 = 'inputlist[' + 0 + '].value'
    var value = 'inputlist[' + 1 + '].value'
    var placeholder1 = 'inputlist[' + 0 + '].placeholder'
    var placeholder = 'inputlist[' + 1 + '].placeholder'
    //新复制 清空input框
    var newnumber = ''
    arr.push(parseInt(e.detail.value))
    if (arr.length == 2) {
      console.log(arr)
      this.setData({
        arrs: arr,
        message: '',
        [placeholder1]:arr[0],
        [placeholder]:arr[1],
        [value1]: arr[0],
        [value]: arr[1]
      })
      newnumber = arr[0].toString() + arr[1].toString()
      //判断第一次输入的高低
      if (parseInt(one.toString() + two.toString()) == parseInt(newnumber)) {
        this.setData({
          message: '猜对了',
        })
        let rewad = 10 - this.data.miaoshu
        this.setData({
          rewad: rewad,
          rewardNum_one: parseInt(one),
          rewardNum_two: parseInt(two),
        })
        this.getCoupon(this.data.rewad)
      }
      if (parseInt(one.toString() + two.toString()) > parseInt(newnumber)) {
        this.setData({
          message: '低了',
          rewardNum_one: parseInt(one),
          rewardNum_two: parseInt(two),
          [value1]: '',
          [value]: '',
          arrs: []
        })
      }
      if (parseInt(one.toString() + two.toString()) < parseInt(newnumber)) {
        this.setData({
          message: '高了',
          rewardNum_one: parseInt(one),
          rewardNum_two: parseInt(two),
          [value1]: '',
          [value]: '',
          arrs: []
        })
      }

    }


    //判断光标在那个
    if (list[0].focus == true) {
      this.setData({
        [indexv]: 'true',
        [indexv1]: 'false',
      })
    } else {
      this.setData({
        [indexv]: 'false',
        [indexv1]: 'true',
      })
    }

  },
  // 点击关闭 全为 0
  cloth: function () {
    //给两个数复制
    var arr = []
    arr.push(this.data.rewardNum_one)
    var arrs = []
    arrs.push(this.data.rewardNum_two)
    //全部初始化
    this.setData({
      cloth: 0,
      miaoshu: 3,
      inputState: 0,
      message: '',
      state: 0,
      arrs: [],
      output: 0,
      rewad: 100,
      min:arr,
      max:arrs,
      marginTop:'0rpx',
      inputlist: [{ "placeholder": "0", focus: true, value: '' }, { "placeholder": "0", focus: false, value: '' }],
    })
    //清空动画的位移
    this.animation.translate(0, (0)).step()
    this.setData({ animation: this.animation.export() })
    this.animation1.translate(0, (0)).step()
    this.setData({ animation1: this.animation1.export() })
  },
  // 点击马上使用
  shiyong: function () {
    wx.navigateTo({
      url: '../taobao_ling/taobao_ling',
    })
  },
  // 海报
  posters: function () {
    wx.navigateTo({
      url: '../posters/posters',
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    /*先放这里验证接口，需要前端接入逻辑 */
    // this.getCoupon();

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 获取奖券
   */
  getCoupon: function (reward) {
    console.log(reward)
    if (reward != 100) {
      this.setData({
        cloth: 1,
        miaoshu: 3,
        inputState: 0,
        message: '',
        state: 0,
        marginTop:'-170rpx'
      })
    }
    var that = this;
    var commodityInfo = that.data.commodityInfo;
    wx.request({
      url: app.globalData.url + "/applet/getCoupon",
      data: {
        memberId: wx.getStorageSync('memberInfo').id,
        timeNumber: reward,//猜中时间
        commodityId: commodityInfo.commodity.id,
        gameId: commodityInfo.game.id,
        // parentId: wx.getStorageSync('parentId')
      },
      success: function (res) {

        console.log(res.data);
        that.setData({
          detail: res.data.data,
          min: [1],
          max: [2],
        })
        wx.setStorageSync('detail', that.data.detail)
      }
    })

    //  使手机振动15ms       使手机振动400ms
    //   wx.vibrateShort();   wx.vibrateLong(); 

  }

})